﻿<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars2d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <link rel="shortcut icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/muyao1987/cdn/marsgis.cn/favicon/favicon.ico" />

    <meta name="keywords" content="火星科技,leaflet,mars2d,GIS,marsgis,二维,地图,地球,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 leaflet leaflet开发 leaflet框架 二维 地图 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 二维地图 全景漫游 地理信息系统 云GIS 二维GIS GIS平台 WebGIS"
    />

    <title>Mars2D地图 |二维地图| 火星科技|合肥火星科技|合肥火星科技有限公</title>

    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,bootstrap-checkbox,layer,haoutil,mars2d"
      ></script>
    <link href="css/style.css" rel="stylesheet" />

    <script src="./plugins/_vector/Semicircle.js"></script>
  </head>
  <body>
    <div id="mars2dContainer" class="mars2d-container"></div>

    <div class="infoview">
      <div>
        图层状态：
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkShow" class="styled" type="checkbox" checked />
          <label for="chkShow"> 显示隐藏 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkPopup" class="styled" type="checkbox" checked />
          <label for="chkPopup"> Popup绑定 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkTooltip" class="styled" type="checkbox" />
          <label for="chkTooltip"> Tooltip绑定 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkContextMenu" class="styled" type="checkbox" checked />
          <label for="chkContextMenu"> 右键菜单绑定 </label>
        </div>
      </div>
      <div>
        数据维护：
        <input id="btnStartDraw" type="button" class="btn btn-primary" value="图上标绘" title="在图上绘制新增一个矢量数据" />

        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkHasEdit" class="styled" type="checkbox" />
          <label for="chkHasEdit"> 是否编辑 </label>
        </div>
      </div>
      <div>
        数据管理：
        <input id="btnClear" type="button" class="btn btn-danger" value="清除" />
        <input id="btnExpFile" type="button" class="btn btn-primary" value="保存GeoJSON" title="保存为GeoJSON文件" />
        <input id="btnImpFile" type="button" class="btn btn-primary" value="打开GeoJSON" title="打开之前保存的GeoJSON文件" />

        <input id="input_draw_file" type="file" accept=".json,.geojson" style="display: none" />
      </div>
    </div>


    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        let mapOptions = mars2d.Util.merge(options, {});

        //创建地图
        map = new mars2d.Map("mars2dContainer", mapOptions);

        map.setView([30.7, 115.6], 10);

        //创建矢量数据图层
        let graphicLayer = new mars2d.layer.GraphicLayer();
        map.addLayer(graphicLayer);

        //图层管理的相关处理，
        initLayerManager(graphicLayer);

        //新增绘制
        $("#btnStartDraw").click(function () {
          //开始绘制
          graphicLayer.startDraw({
            type: "circle",
            style: {
              startAngle: 0,
              stopAngle: 60,
              rotation: 0,

              fill: true,
              fillColor: "#000dfc",
              fillOpacity: 0.3,
              outline: true,
              outlineWidth: 2,
              outlineColor: "#254dc4",
              outlineOpacity: 1,
            },
            success: function (graphic) {
              console.log("标绘完成", graphic);
            },
          });
        });

        //加一些演示数据
        addGraphic_01(graphicLayer);
        addGraphic_02(graphicLayer);
        addGraphic_03(graphicLayer);
        addGraphic_04(graphicLayer);
        addGraphic_05(graphicLayer);
      }

      //示例1：{startAngle: x, stopAngle: y}
      function addGraphic_01(graphicLayer) {
        for (let i = 0; i <= 12; i++) {
          let graphic = new mars2d.graphic.Circle({
            latlng: [31, 115 + i * 0.1],
            style: {
              radius: 4000, //单位：米
              startAngle: 0,
              stopAngle: 30 * i,
              rotation: 0,

              fill: true,
              fillColor: "#f03",
              fillOpacity: 0.7,
              outline: true,
              outlineWidth: 2,
              outlineColor: "#00ff00",
              outlineOpacity: 0.5,
            },
          });
          graphicLayer.addGraphic(graphic);

          graphic.bindPopup(`角度：${graphic.startAngle}至${graphic.stopAngle}`);
        }
      }

      //固定的张角
      function addGraphic_02(graphicLayer) {
        for (let i = 0; i <= 12; i++) {
          let graphic = new mars2d.graphic.Circle({
            latlng: [30.5, 115 + i * 0.1],
            style: {
              radius: 4000, //单位：米
              startAngle: 0,
              stopAngle: 60,
              rotation: i * 30,

              fill: true,
              fillColor: "#03f",
              fillOpacity: 0.7,
              outline: true,
              outlineWidth: 2,
              outlineColor: "#00ff00",
              outlineOpacity: 0.5,
            },
          });
          graphicLayer.addGraphic(graphic);

          graphic.bindPopup(`角度：${graphic.startAngle}至${graphic.stopAngle}<br/>方向：${graphic.rotation}`);
        }
      }

      //示例3：吃豆豆
      function addGraphic_03(graphicLayer) {
        let graphic = new mars2d.graphic.Circle({
          latlng: [30.7, 115.2],
          style: {
            radius: 5000, //单位：米
            startAngle: -135,
            stopAngle: 135,
            rotation: 0,

            fill: true,
            fillColor: "#ff0",
            fillOpacity: 0.9,
            outline: true,
            outlineColor: "#ff0000",
            outlineOpacity: 1.0,
            outlineWidth: 2,
          },
        });
        graphicLayer.addGraphic(graphic);

        let open;
        let counter = 1;
        setInterval(function () {
          counter += 0.5;
          open = 358 - (1 + Math.sin(counter)) * 45;

          graphic.startAngle = -open / 2;
          graphic.stopAngle = +open / 2;
        }, 80);
      }

      //组合：雷达线扇形
      function addGraphic_04(graphicLayer) {
        let interval = 1000;
        for (let i = 1; i <= 8; i++) {
          let graphic = new mars2d.graphic.Circle({
            latlng: [30.7, 115.5],
            style: {
              radius: i * interval,
              startAngle: 0,
              stopAngle: 120,
              rotation: -60,

              fill: false,
              outline: true,
              outlineWidth: 1,
              outlineColor: "#000",
              outlineOpacity: 1,
            },
          });
          graphicLayer.addGraphic(graphic);
        }
      }

      //组合：3各圆辐射标志组合
      function addGraphic_05(graphicLayer) {
        let latlng = [30.7, 115.8];
        let radius = 5000;

        let graphic = new mars2d.graphic.Circle({
          latlng: latlng,
          style: {
            radius: radius,
            fill: true,
            fillColor: "#ffffff",
            fillOpacity: 0.5,
            outline: true,
            outlineColor: "red",
          },
        }).addTo(graphicLayer);

        let graphic1 = new mars2d.graphic.Circle({
          latlng: latlng,
          style: {
            radius: radius,
            startAngle: 0,
            stopAngle: 45,
            fill: true,
            fillColor: "red",
            fillOpacity: 0.8,
            outline: false,
          },
        }).addTo(graphicLayer);

        let graphic2 = new mars2d.graphic.Circle({
          latlng: latlng,
          style: {
            radius: radius,
            startAngle: 120,
            stopAngle: 120 + 45,
            fill: true,
            fillColor: "red",
            fillOpacity: 0.8,
            outline: false,
          },
        }).addTo(graphicLayer);

        let graphic3 = new mars2d.graphic.Circle({
          latlng: latlng,
          style: {
            radius: radius,
            startAngle: 240,
            stopAngle: 240 + 45,
            fill: true,
            fillColor: "red",
            fillOpacity: 0.8,
            outline: false,
          },
        }).addTo(graphicLayer);
      }

      //在图层级处理一些事物
      function initLayerManager(graphicLayer) {
        //在layer上绑定监听事件
        graphicLayer.on(mars2d.EventType.click, function (event) {
          console.log("监听layer，单击了矢量对象", event);
        });

        //可在图层上绑定popup,对所有加到这个图层的矢量数据都生效
        bindLayerPopup(graphicLayer);

        $("#chkPopup").change(function () {
          let val = $(this).is(":checked");

          if (val) {
            bindLayerPopup(graphicLayer);
          } else {
            graphicLayer.unbindPopup();
          }
        });

        //可在图层上绑定tooltip,对所有加到这个图层的矢量数据都生效
        $("#chkTooltip").change(function () {
          let val = $(this).is(":checked");

          if (val) {
            graphicLayer.bindTooltip("我是layer上绑定的Tooltip");
          } else {
            graphicLayer.unbindTooltip();
          }
        });

        //可在图层绑定右键菜单,对所有加到这个图层的矢量数据都生效
        bindLayerContextMenu(graphicLayer);
        $("#chkContextMenu").change(function () {
          let val = $(this).is(":checked");

          if (val) {
            bindLayerContextMenu(graphicLayer);
          } else {
            graphicLayer.unbindContextMenu(true);
          }
        });

        $("#chkHasEdit").change(function () {
          let val = $(this).is(":checked");

          graphicLayer.hasEdit = val; //启用编辑
        });

        $("#chkShow").change(function () {
          let val = $(this).is(":checked");

          graphicLayer.show = val; //显示隐藏
        });

        $("#btnClear").click(function () {
          graphicLayer.clear();
        });

        $("#btnExpFile").click(function () {
          if (graphicLayer.length === 0) {
            window.layer.msg("当前没有标注任何数据，无需保存！");
            return;
          }

          let geojson = graphicLayer.toGeoJSON();
          haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson));
        });

        $("#btnImpFile").click(function () {
          $("#input_draw_file").click();
        });

        function clearSelectFile() {
          if (!window.addEventListener) {
            document.getElementById("input_draw_file").outerHTML += ""; //IE
          } else {
            document.getElementById("input_draw_file").value = ""; //FF
          }
        }

        $("#input_draw_file").change(function (e) {
          let file = this.files[0];

          let fileName = file.name;
          let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();

          if (fileType == "json" || fileType == "geojson") {
            let reader = new FileReader();
            reader.readAsText(file, "UTF-8");
            reader.onloadend = function (e) {
              let json = this.result;
              graphicLayer.loadGeoJSON(json, {
                flyTo: true,
              });
              clearSelectFile();
            };
          } else {
            window.layer.msg("暂不支持 " + fileType + " 文件类型的数据！");
            clearSelectFile();
          }
        });
      }

      function bindLayerPopup(graphicLayer) {
        graphicLayer.bindPopup(function (event) {
          let attr = event.graphic?.attr || {};
          attr.test1 = "测试属性";
          // attr["视频"] = `<video src='http://data.mars2d.cn/file/video/lukou.mp4' controls autoplay style="width: 300px;" ></video>`;

          return mars2d.Util.getTemplateHtml({ title: "layer上绑定的Popup", template: "all", attr: attr });
        });
      }

      function bindLayerContextMenu(graphicLayer) {
        graphicLayer.bindContextMenu([
          {
            text: "开始编辑对象",
            iconCls: "fa fa-edit",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic || !graphic.editing) {
                return false;
              }
              return !graphic.editing.enabled();
            },
            callback: function (e) {
              let graphic = e.graphic;
              if (graphic && graphic.editing) {
                graphic.editing.enable();
              }
            },
          },
          {
            text: "停止编辑对象",
            iconCls: "fa fa-edit",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic || !graphic.editing) {
                return false;
              }
              return graphic.editing.enabled();
            },
            callback: function (e) {
              let graphic = e.graphic;
              if (graphic && graphic.editing) {
                graphic.editing.disable();
              }
            },
          },
          {
            text: "删除对象",
            iconCls: "fa fa-trash-o",
            show: (event) => {
              let graphic = event.graphic;
              if (!graphic) {
                return false;
              } else {
                return true;
              }
            },
            callback: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return;
              }
              graphicLayer.removeGraphic(graphic);
            },
          },
          {
            text: "计算长度",
            iconCls: "fa fa-medium",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return false;
              }
              return graphic.type === "polyline" || graphic.type === "brushLine";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strDis = mars2d.MeasureUtil.formatDistance(graphic.distance);
              haoutil.alert("该对象的长度为:" + strDis);
            },
          },
          {
            text: "计算周长",
            iconCls: "fa fa-medium",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return false;
              }
              return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strDis = mars2d.MeasureUtil.formatDistance(graphic.distance);
              haoutil.alert("该对象的周长为:" + strDis);
            },
          },
          {
            text: "计算面积",
            iconCls: "fa fa-reorder",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return false;
              }
              return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strArea = mars2d.MeasureUtil.formatArea(graphic.area);
              haoutil.alert("该对象的面积为:" + strArea);
            },
          },
        ]);
      }
     </script>
  </body>
</html>
